﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WFI.common.Extension;
using Models.Extension;

namespace Common.CacheCleient
{
    /// <summary>
    /// 客户端缓存方法
    /// </summary>
    public static class cacheManage
    {
       

        #region 公用方法

        /// <summary>
        /// 得到缓存数据 方法无参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="wsMethod"></param>
        /// <returns></returns>
        public static List<T> GetDataWithCache<T>(CacheEntity<T> entity, Func<IList<T>> wsMethod)
        {
            if (entity.EntityValue == null||entity.isQuery||entity.isQueryEvery)  //实体为空 或 ISquery=True 则执行
            {
                SetData<T>(entity, wsMethod);
                entity.isQuery = false;
            }
            else
            {
                if (DateTime.Now > entity.NextUpdateTime)
                    SetData<T>(entity, wsMethod);
            }
            return entity.EntityValue.Clone() as List<T>;
        }

        public static string GetDataWithCache_ByParaObject<T>(CacheEntity<T> entity, Func<string, string> wsMethod, string key)
        {
            if (string.IsNullOrEmpty(entity.EntityStringValue) || entity.isQuery||entity.isQueryEvery)
            {
                SetData_ByParaObject<T>(entity, wsMethod, key);
                entity.isQuery = false;
            }
            else
            {
                if (DateTime.Now > entity.NextUpdateTime)
                    SetData_ByParaObject<T>(entity, wsMethod, key);
            }
            return entity.EntityStringValue.Clone() as string;
        }

        private static void SetData_ByParaObject<T>(CacheEntity<T> entity, Func<string, string> wsMethod, string key)
        {
            string lst = wsMethod(key);
            BindEntityObject<T>(entity, lst);
        }
        private static void BindEntityObject<T>(CacheEntity<T> entity, string lst)
        {
            if (string.IsNullOrEmpty(lst))
            {
                entity.EntityStringValue = "";
            }
            else
            {
                entity.EntityStringValue = lst;
            }
            entity.NextUpdateTime = DateTime.Now.AddMinutes(entity.AddMinute);
        }

        /// <summary>
        /// 填充数据到缓存实体， 范型委托实现，WebService方法的传递
        /// </summary>
        private static void SetData<T>(CacheEntity<T> entity, Func<IList<T>> wsMethod)
        {
            IList<T> lst = wsMethod();
            BindEntity<T>(entity, lst);
        }

        private static List<T> GetDataWithCache_ByParaInt<T>(CacheEntity<T> entity, Func<int, T[]> wsMethod, int para)
        {
            if (entity.EntityValue == null)
            {
                SetData_ByParaInt<T>(entity, wsMethod, para);
            }
            else
            {
                if (DateTime.Now > entity.NextUpdateTime)
                    SetData_ByParaInt<T>(entity, wsMethod, para);
            }
            return entity.EntityValue.Clone() as List<T>;
        }

        private static void SetData_ByParaInt<T>(CacheEntity<T> entity, Func<int, T[]> wsMethod, int para)
        {
            T[] lst = wsMethod(para);
            BindEntity<T>(entity, lst);
        }

        private static void BindEntity<T>(CacheEntity<T> entity, IList<T> lst)
        {
            if (lst == null)
            {
                entity.EntityValue = new List<T>();
            }
            else
            {
                entity.EntityValue = lst.ToList();
            }
            entity.NextUpdateTime = DateTime.Now.AddMinutes(entity.AddMinute);
        }

        #endregion
    }

   
}
